package cn.virens.library.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import org.xutils.x;

import java.io.Serializable;

/**
 * 基础 页面
 */
public class VActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        beforeViewInject(savedInstanceState);
        x.view().inject(this);
    }

    /**
     * 视图注入之前
     *
     * @param savedInstanceState
     */
    protected void beforeViewInject(Bundle savedInstanceState) {
    }

    /**
     * 获取 - 序列化参数
     *
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    protected <T> T getExtra(String name, Class<T> clazz) {
        Serializable tmp = getIntent().getSerializableExtra(name);

        if (tmp != null && clazz.isAssignableFrom(tmp.getClass())) {
            return (T) tmp;
        } else {
            return null;
        }
    }

    /**
     * 获取 - 序列化参数
     *
     * @param name
     * @param defaultValue
     * @param <T>
     * @return
     */
    protected <T> T getExtraDef(String name, T defaultValue) {
        if (defaultValue == null) throw new IllegalArgumentException("defaultValue is not null");

        Object result = getExtra(name, defaultValue.getClass());

        return result != null ? (T) result : defaultValue;
    }

    /**
     * 获取 - 字符串参数
     *
     * @param name
     * @return
     */
    protected String getStringExtra(String name) {
        return getIntent().getStringExtra(name);
    }

    /**
     * 获取 - 布尔值参数
     *
     * @param name
     * @return
     */
    protected boolean getBooleanExtra(String name) {
        return getIntent().getBooleanExtra(name, false);
    }

    /**
     * 获取 - Int类型参数
     *
     * @param name
     * @param defaultValue
     * @return
     */
    protected int getIntExtra(String name, int defaultValue) {
        return getIntent().getIntExtra(name, defaultValue);
    }
}
